'use strict';
app.controller('goodsManageCtrl',['$scope','$cookies','utilService','$modal','$stateParams',
    function($scope,$cookies,utilService,$modal,$stateParams) {
    $scope.goods = {
        sessionId :$cookies.sessionId
    };
    //定义联想查询
    $scope.states = [];
    $scope.shopName = '';
    $scope.queryShopGoods = function () {
        if($scope.shopName == ''){
            $scope.goods.shopId = null;
        }
        for (var key in $scope.states) {
            if($scope.states[key].name  == $scope.shopName){
                $scope.goods.shopId = $scope.states[key].shopId;
            }
        }
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'chain/goods',
            params:$scope.goods
        }).then(function (result) {
        });
    }
    $scope.queryShopGoods();

    //联想查询请求函数 
    $scope.getLocation = function (val) {
        return utilService.get({
            url: $scope.app.domain + 'shop',
            params: {name:val}
        }).then(function (response) {
            $scope.states = response.data.list;
            return response.data.list;
        }, function (e) {
            deferred.reject("连接服务器出错！");
        });
    };

}]);


//强制下架
app.controller('goodsManageSoldOutCtrl',['$scope','$stateParams','chainGoodsManageService','toaster','$state',
    function($scope,$stateParams,chainGoodsManageService,toaster,$state) {
    $scope.chainGoods={
        id:$stateParams.id
    }
    $scope.changeStatus=function(){
        chainGoodsManageService.changeStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    history.back();
                    break;
                case 673:
                    toaster.pop('error', "提示", "提示：该商品并未上架！");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
}]);